package leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class CompareVersion {

    public static void main(String[] args) {

        //int res = compareVersion("0.1", "1.1");

        //System.out.println(res);

        //System.out.println(reverseString("hello"));
        //System.out.println(reverseWords("hello world"));
        /*int[] nums = {1, 2, 3, 4};
        productExceptSelf(nums);*/

//        System.out.println(isPowerOfTwo(64));
        System.out.println(countDigitOne(11));
    }

    public static int countDigitOne(int n) {

        int num = 0;
        String s;
        char c = '1';
        for (int i = 1; i <= n; i++) {
            s = String.valueOf(i);
            if (s.indexOf("1") >= 0) {
                for (int j = 0; j < s.length(); j++) {
                    if (s.charAt(j) == c) {
                        num += 1;
                    }
                }
            }
        }
        return num;
    }

    public static int findDuplicate(int[] nums) {

        for (int i = 0; i < nums.length; i++) {
            for (int j = 0; j < nums.length; j++) {
                if (i != j && nums[i] == nums[j]) {
                    return nums[i];
                }
            }
        }
        throw new RuntimeException("");
    }


    public static boolean isPowerOfTwo(int n) {
        for (int i = 0; i < n; i++) {
            double d = Math.pow(2, i);
            if (d < n) {
                continue;
            }
            if (d > n) {
                return false;
            }
            return true;
        }
        return true;
    }

    /**
     * 判断数组中是否有重复
     *
     * @param nums
     * @return
     */
    public boolean containsDuplicate(int[] nums) {
        int len = Arrays.stream(nums).distinct().toArray().length;
        return len != nums.length;
    }


    /**
     * 版本号比较
     *
     * @param version1
     * @param version2
     * @return
     */
    public static int compareVersion(String version1, String version2) {
        String[] v1 = version1.split("\\.");
        String[] v2 = version2.split("\\.");


        int len = v1.length >= v2.length ? v1.length : v2.length;
        int a, b;
        for (int i = 0; i < len; i++) {
            a = 0;
            b = 0;
            if (v1.length > i) {
                a = Integer.parseInt(v1[i]);
            }
            if (v2.length > i) {
                b = Integer.parseInt(v2[i]);
            }
            if (a > b) {
                return 1;
            } else if (a < b) {
                return -1;
            }
        }
        return 0;

    }

    /**
     * 字符串反转
     *
     * @param s
     * @return
     */
    public static String reverseString(String s) {


        char[] chars = s.toCharArray();
        int len = chars.length;
        char[] res = new char[len];

        for (int i = chars.length - 1; i >= 0; i--) {
            res[len - i - 1] = chars[i];
        }

        return String.valueOf(res);
    }

    public static String reverseWords(String s) {

        String[] strs = s.split(" ");
        char[] chars;
        int len;
        char[] res;
        for (int i = 0; i < strs.length; i++) {
            chars = strs[i].toCharArray();
            len = chars.length;
            res = new char[len];

            for (int j = chars.length - 1; j >= 0; j--) {
                res[len - j - 1] = chars[j];
            }
            strs[i] = String.valueOf(res);
        }
        return String.join(" ", strs);
    }

    public static int[] productExceptSelf(int[] nums) {

        int[] sums = new int[nums.length];
        for (int i = 0; i < nums.length; i++) {
            int sum = 1;
            for (int j = 0; j < nums.length; j++) {
                if (i == j) {
                    continue;
                }
                sum *= nums[j];
            }
            sums[i] = sum;
        }
        return sums;
    }


}
